﻿using B.S.DataBase.MyProject.Domain.DiagnosisAndTreatmentManagement;
using B.S.DataBase.MyProject.Domain.HealthMonitoring;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure;
using MediatR;
using MedicalPCTerminal.Read.API.Appcation.Commands.HealthMonitoringCommand;
using System.Data.OscarClient;

namespace MedicalPCTerminal.Read.API.Appcation.CommandHandlers.HealthMonitoringCommandHandler
{
    /// <summary>
    /// BMI
    /// </summary>
    public class GetBMICommandHandler : IRequestHandler<GetBMICommand, APIResult<List<BMIMonitoring>>>
    {
        private readonly SqlSugarContext sqlSugar;

        public GetBMICommandHandler(SqlSugarContext sqlSugar)
        {
            this.sqlSugar = sqlSugar;
        }

        public async Task<APIResult<List<BMIMonitoring>>> Handle(GetBMICommand request, CancellationToken cancellationToken)
        {
            APIResult<List<BMIMonitoring>> result = new APIResult<List<BMIMonitoring>>();
            var entity = sqlSugar.db.Queryable<BMIMonitoring>().Where(x => x.UserId == request.UserId);
            entity = entity.OrderByDescending(x => x.BId);
            if (entity != null)
            {
                result.Code = APIEnum.查询成功;
                result.Msg = APIEnum.查询成功.ToString();
                result.data = entity.ToList();
            }
            else
            {
                result.Code = APIEnum.查询失败;
                result.Msg = APIEnum.查询失败.ToString();
            }
            return await Task.FromResult(result);
        }
    }
}
